<?php
/*
 * This file is part of the "nimo-l4" package.
 *
 * (c) Enkhbayasgalan Galsandorj <eegii2you@yahoo.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace nimo\domains\car_balance\controllers;

use BaseController;
use Input;
use nimo\domains\car_balance\Model\Customer;
use nimo\domains\car_balance\Model\Ems;
use nimo\domains\car_balance\Model\MoneyTransfer;
use nimo\domains\car_balance\Model\Parts;
use nimo\domains\car_balance\Model\PurchaseCar;
use nimo\domains\car_balance\Model\ServiceCompany;
use nimo\domains\car_balance\service\SummaryCalcService;
use Request;
use Session;
use View;
use Carbon\Carbon;

class SummaryController extends BaseController
{
    public function mainSummary()
    {
        $this->checkPermission('admin');

        if(Request::isMethod('POST')) {
            // Filter form values to session
            $purchase_date = is_null(Input::get('purchase_date')) ? array() : explode(' to ',Input::get('purchase_date'));
            if (Input::has('dateS')) {
              $purchase_date[0] = Input::get('dateS');
              $purchase_date[1] = Input::get('dateE');
            }
            Session::put('summary.filter.purchase_date', $purchase_date);
            $service_company_id = is_null(Input::get('service_company_id')) ? '0' : Input::get('service_company_id');
            Session::put('summary.filter.service_company_id', $service_company_id);
            $customer_id = is_null(Input::get('customer_id')) ? '' : Input::get('customer_id');
            Session::put('summary.filter.customer_id', $customer_id);
            $balance = is_null(Input::get('balance')) ? 'Y' : Input::get('balance');
            Session::put('summary.filter.balance', $balance);
        } else {
            // Filter values from session
            $purchase_date = Session::get('summary.filter.purchase_date', array());
            $service_company_id = Session::get('summary.filter.service_company_id', '0');
            $customer_id = Session::get('summary.filter.customer_id', '0');
            $balance = Session::get('summary.filter.balance', 'Y');
        }
        // Car
        $query = new PurchaseCar();
        if(count($purchase_date) < 2) {
            $now = Carbon::now();
            // balance year
            $now = ($now->month > 9) ? $now : $now->subYear(1);
            $now = $now->month(10)->day(1);
            $purchase_date[0] = $now->format('Y-m-d');
            $purchase_date[1] = $now->addYear(1)->format('Y-m-d');
        }
        if(count($purchase_date) >1) {
            $purchase_date_between = array($purchase_date[0], $purchase_date[1]);
        }
        if($purchase_date_between)
            $query = $query->whereBetween('purchase_date', $purchase_date_between);
        if(!empty($customer_id) )
            $query = $query->where('customer_id', '=', $customer_id);
        if($service_company_id != 0)
            $query = $query->where('service_company_id', '=', $service_company_id);
        $cars = $query->get();
        $l = Input::get('l');
        if (!empty($l)) {
          $summaries = SummaryCalcService::summary($cars);
          $summariesDocs = SummaryCalcService::summaryDocuments($cars);
        } else {
          $summaries = SummaryCalcService::summary_between($cars, $purchase_date);
          $summariesDocs = $summaries;
        }

        // Parts
        $parts_query = new Parts();
        if($purchase_date_between)
            $parts_query = $parts_query->whereBetween('purchase_date', $purchase_date_between);
        if(!empty($customer_id) )
            $parts_query = $parts_query->where('customer_id', '=', $customer_id);
        $parts = $parts_query->get();
        $summariesParts = SummaryCalcService::summaryParts($parts);
        /*
        $ems_query = new Ems();
        if($purchase_date_between)
            $ems_query = $ems_query->whereBetween('send_date', $purchase_date_between);
        $ems_count = $ems_query->count();
        $ems_sum = $ems_count > 0 ? $ems_query->sum('fee_japan') : 0;

        $mt_query = new MoneyTransfer();
        if($purchase_date_between)
            $mt_query = $mt_query->whereBetween('transfer_date', $purchase_date_between);
        $mt_count = $mt_query->count();
        $mt_sum = $mt_count > 0 ? $mt_query->sum('commission') : 0;
        */


        $filters = array(
            'purchase_date' => $purchase_date,
            'customer_id' => $customer_id,
            'service_company_id' => $service_company_id,
            'balance' => $balance,
        );

        return View::make('nimo.domains.car_balance.Summary.main'.$l)
            ->with('cars', $cars)
            ->with('parts', $parts)
            ->with('filters', $filters)
            ->with('service_company_list', array_merge(array('0' => ''), ServiceCompany::lists('name', 'id')))
            ->with('customer_list', array_merge(array('0' => ''), Customer::lists('name', 'id')))
            /*
            ->with('ems_sum', $ems_sum)
            ->with('ems_count', $ems_count)
            ->with('mt_sum', $mt_sum)
            ->with('mt_count', $mt_count)
            */
            ->with('summaries', $summaries)
            ->with('summariesDocs', $summariesDocs)
            ->with('summariesParts', $summariesParts)
            ;

    }
    public function mainSummary2()
    {
        $this->checkPermission('admin');

        if(Request::isMethod('POST')) {
            // Filter form values to session
            $purchase_date = is_null(Input::get('purchase_date')) ? array() : explode(' to ',Input::get('purchase_date'));
            if (Input::has('dateS')) {
              $purchase_date[0] = Input::get('dateS');
              $purchase_date[1] = Input::get('dateE');
            }
            Session::put('summary.filter.purchase_date', $purchase_date);
            $service_company_id = is_null(Input::get('service_company_id')) ? '0' : Input::get('service_company_id');
            Session::put('summary.filter.service_company_id', $service_company_id);
            $customer_id = is_null(Input::get('customer_id')) ? '' : Input::get('customer_id');
            Session::put('summary.filter.customer_id', $customer_id);
            $balance = is_null(Input::get('balance')) ? 'Y' : Input::get('balance');
            Session::put('summary.filter.balance', $balance);
        } else {
            // Filter values from session
            $purchase_date = Session::get('summary.filter.purchase_date', array());
            $service_company_id = Session::get('summary.filter.service_company_id', '0');
            $customer_id = Session::get('summary.filter.customer_id', '0');
            $balance = Session::get('summary.filter.balance', 'Y');
        }
        // Car
        $query = new PurchaseCar();
        if(count($purchase_date) < 2) {
            $now = Carbon::now();
            // balance year
            $now = ($now->month > 9) ? $now : $now->subYear(1);
            $now = $now->month(10)->day(1);
            $purchase_date[0] = $now->format('Y-m-d');
            $purchase_date[1] = $now->addYear(1)->format('Y-m-d');
        }
        if(count($purchase_date) >1) {
            $purchase_date_between = array($purchase_date[0], $purchase_date[1]);
        }
        if($purchase_date_between)
            $query = $query->whereBetween('purchase_date', $purchase_date_between);
        if(!empty($customer_id) )
            $query = $query->where('customer_id', '=', $customer_id);
        if($service_company_id != 0)
            $query = $query->where('service_company_id', '=', $service_company_id);
        $cars = $query->get();
        $l = Input::get('l');
        if (!empty($l)) {
          $summaries = SummaryCalcService::summary($cars);
          $summariesDocs = SummaryCalcService::summaryDocuments($cars);
        } else {
          $summaries = SummaryCalcService::summary_between($cars, $purchase_date);
          $summariesDocs = $summaries;
        }

        // Parts
        $parts_query = new Parts();
        if($purchase_date_between)
            $parts_query = $parts_query->whereBetween('purchase_date', $purchase_date_between);
        if(!empty($customer_id) )
            $parts_query = $parts_query->where('customer_id', '=', $customer_id);
        $parts = $parts_query->get();
        $summariesParts = SummaryCalcService::summaryParts($parts);
        /*
        $ems_query = new Ems();
        if($purchase_date_between)
            $ems_query = $ems_query->whereBetween('send_date', $purchase_date_between);
        $ems_count = $ems_query->count();
        $ems_sum = $ems_count > 0 ? $ems_query->sum('fee_japan') : 0;

        $mt_query = new MoneyTransfer();
        if($purchase_date_between)
            $mt_query = $mt_query->whereBetween('transfer_date', $purchase_date_between);
        $mt_count = $mt_query->count();
        $mt_sum = $mt_count > 0 ? $mt_query->sum('commission') : 0;
        */


        $filters = array(
            'purchase_date' => $purchase_date,
            'customer_id' => $customer_id,
            'service_company_id' => $service_company_id,
            'balance' => $balance,
        );

        return View::make('nimo.domains.car_balance.Summary.main'.$l)
            ->with('cars', $cars)
            ->with('parts', $parts)
            ->with('filters', $filters)
            ->with('service_company_list', array_merge(array('0' => ''), ServiceCompany::lists('name', 'id')))
            ->with('customer_list', array_merge(array('0' => ''), Customer::lists('name', 'id')))
            /*
            ->with('ems_sum', $ems_sum)
            ->with('ems_count', $ems_count)
            ->with('mt_sum', $mt_sum)
            ->with('mt_count', $mt_count)
            */
            ->with('summaries', $summaries)
            ->with('summariesDocs', $summariesDocs)
            ->with('summariesParts', $summariesParts)
            ;

    }

}